﻿using Hhmocon.Mes.Application.Common;
using Hhmocon.Mes.Application.Response;
using Hhmocon.Mes.Repository;
using Hhmocon.Mes.Repository.Domain;
using Hhmocon.Mes.Repository.Domain.Energy;
using Hhmocon.Mes.Repository.Repository;
using Hhmocon.Mes.Repository.Response;
using Hhmocon.Mes.Util;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hhmocon.Mes.Application.Energy
{
   public class EnergyConsumptionApp
    {
        private PikachuRepository _pikachuRepository;
        private IEnergyConsumptionRepository  _energyConsumptionRepository;

        public EnergyConsumptionApp(PikachuRepository pikachuRepository, IEnergyConsumptionRepository  energyConsumptionRepository)
        {
            _pikachuRepository = pikachuRepository;
            _energyConsumptionRepository = energyConsumptionRepository;
        }

        public energy_consumption Insert(energy_consumption data)
        {
            //取ID
            data.energy_consumption_id = CommonHelper.GetNextGUID();
            data.modified_time = Time.Now;
            data.create_time = DateTime.Now;
            if (_pikachuRepository.Insert(data))
            {
                return data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// energy_carbon_emission的新增
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public energy_carbon_emission InsertCarbon(energy_carbon_emission data)
        {
            //取ID
            data.energy_consumption_id = CommonHelper.GetNextGUID();
            data.energy_carbon_emission_id = CommonHelper.GetNextGUID();
            data.modified_time = Time.Now;
            data.create_time = DateTime.Now;
            if (_pikachuRepository.Insert(data))
            {
                return data;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// xx碳排放总量页面接口  时间前端只传年月日  TimeCode 年0月1
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public GetCarbonEmissionResponseTan GetEnergyConsumption(DateTime EndTime, int TimeCode)
        {
            if (TimeCode != 1 || TimeCode != 0) TimeCode = 0;

            GregorianCalendar gc = new GregorianCalendar();
            DateTime StartTime = new();
            //如果时间粒度是年，则前三年到上个月的
            if (TimeCode == 0)
            {
                StartTime = new DateTime(EndTime.Year - 3, 01, 01, 00, 00, 00);

                EndTime = new DateTime(EndTime.Year, EndTime.Month > 1 ? EndTime.Month - 1 : EndTime.Month, gc.GetDaysInMonth(EndTime.Year, EndTime.Month > 1 ? EndTime.Month - 1:EndTime.Month), 23, 59, 59);
            }
            //如果时间粒度是月，则1月到上个月的
            else if (TimeCode == 1)
            {
                StartTime = new DateTime(EndTime.Year, 01, 01, 00, 00, 00);
                EndTime = new DateTime(EndTime.Year, EndTime.Month > 1 ? EndTime.Month - 1 : EndTime.Month, gc.GetDaysInMonth(EndTime.Year, EndTime.Month - 1), 23, 59, 59);
            }

            List<energy_carbon_emission> energy_Carbon_Emissions = _energyConsumptionRepository.GetEnergyConsumption(StartTime, EndTime);

            GetCarbonEmissionResponseTan getCarbonEmissionResponseTan = new();

            List<string> gap = new();
            ComputeMouth.ComputeMonth(StartTime.Year, StartTime.Month, EndTime.Year, EndTime.Month);
                    
            //遍历年-月
            foreach (string temp in gap)
            {
                energy_carbon_emission energy_Carbon_Emission = new();
                string year = temp.Split("-")[0];
                string mouth = temp.Split("-")[1];
                List<energy_carbon_emission> energy_Carbon_Emissions1 
                    = energy_Carbon_Emissions.Where(c => c.consumption_date.Year == int.Parse(year) && c.consumption_date.Month == int.Parse(mouth) ).ToList();
                foreach (energy_carbon_emission e in energy_Carbon_Emissions1)
                {
                    energy_Carbon_Emission.carbon_emissions_energy_total += e.carbon_emissions_energy_total;
                    energy_Carbon_Emission.carbon_emissions_total += e.carbon_emissions_energy_total;
                    energy_Carbon_Emission.carbon_reduces_emissions += e.carbon_reduces_emissions;
                  
                }
                getCarbonEmissionResponseTan.carbon_reduces_emissions.Add(energy_Carbon_Emission.carbon_reduces_emissions);
                getCarbonEmissionResponseTan.carbon_emissions_total.Add(energy_Carbon_Emission.carbon_emissions_total);
                getCarbonEmissionResponseTan.carbon_emissions_energy_total.Add(energy_Carbon_Emission.carbon_emissions_energy_total);
            }
            return getCarbonEmissionResponseTan;
        }

        /// <summary>
        /// 碳-碳中和页面
        /// </summary>
        /// <param name="EndTime"></param>
        /// <param name="TimeCode">年0月1</param>
        /// <returns></returns>
        public GetCarbonEmissionResponseTanZhongHe GetCarbonMedium(DateTime EndTime, int TimeCode)
        {
            GregorianCalendar gc = new GregorianCalendar();
            DateTime StartTime = new();
            //如果时间粒度是年，则前三年到上个月的
            if (TimeCode == 0)
            {
                StartTime = new DateTime(EndTime.Year - 3, 01, 01, 00, 00, 00);

                EndTime = new DateTime(EndTime.Year, EndTime.Month > 1 ? EndTime.Month - 1 : EndTime.Month, gc.GetDaysInMonth(EndTime.Year, EndTime.Month > 1 ? EndTime.Month - 1 : EndTime.Month), 23, 59, 59);
            }
            //如果时间粒度是月，则1月到上个月的
            else if (TimeCode == 1)
            {
                StartTime = new DateTime(EndTime.Year, 01, 01, 00, 00, 00);
                EndTime = new DateTime(EndTime.Year, EndTime.Month > 1 ? EndTime.Month - 1 : EndTime.Month, gc.GetDaysInMonth(EndTime.Year, EndTime.Month - 1), 23, 59, 59);
            }

            List<energy_carbon_emission> energy_Carbon_Emissions = _energyConsumptionRepository.getCarbonMedium(StartTime, EndTime);

            GetCarbonEmissionResponseTanZhongHe getCarbonEmissionResponseTanZhongHe = new();

            List<string> gap = ComputeMouth.ComputeMonth(StartTime.Year, StartTime.Month, EndTime.Year, EndTime.Month);
            //遍历年-月
            foreach (string temp in gap)
            {
                energy_carbon_emission energy_Carbon_Emission = new();
                string year = temp.Split("-")[0];
                string mouth = temp.Split("-")[1];
                List<energy_carbon_emission> energy_Carbon_Emissions1
                    = energy_Carbon_Emissions.Where(c => c.consumption_date.Year == int.Parse(year) && c.consumption_date.Month == int.Parse(mouth)).ToList();
                foreach (energy_carbon_emission e in energy_Carbon_Emissions1)
                {
                    //固碳减少碳排放总量
                    energy_Carbon_Emission.carbon_reduces_emissions += e.carbon_reduces_emissions;
                    //零碳电力折减碳排放量
                    energy_Carbon_Emission.carbon_electricity_reduces_zero += e.carbon_electricity_reduces_zero;
                    //碳中和量
                    energy_Carbon_Emission.carbon_neutral += e.carbon_neutral;


                    //碳排放总量
                    energy_Carbon_Emission.carbon_emissions_total += e.carbon_emissions_energy_total;
                    //消耗零碳电力折减碳排放量
                    energy_Carbon_Emission.consume_carbon_electricity_reduces_zero += e.consume_carbon_electricity_reduces_zero;
                    //碳排放折算量
                    energy_Carbon_Emission.carbon_emission_conversion += e.carbon_emission_conversion;

                    //全局碳中和量
                    getCarbonEmissionResponseTanZhongHe.carbon_neutral_global += e.carbon_neutral;
                    //全局碳折算量
                    getCarbonEmissionResponseTanZhongHe.carbon_emission_conversion_global += e.carbon_emission_conversion;

                }
                getCarbonEmissionResponseTanZhongHe.carbon_reduces_emissions.Add(energy_Carbon_Emission.carbon_reduces_emissions);
                getCarbonEmissionResponseTanZhongHe.carbon_electricity_reduces_zero.Add(energy_Carbon_Emission.carbon_electricity_reduces_zero);
                getCarbonEmissionResponseTanZhongHe.carbon_neutral.Add(energy_Carbon_Emission.carbon_neutral);
      
                getCarbonEmissionResponseTanZhongHe.carbon_emissions_total.Add(energy_Carbon_Emission.carbon_emissions_total);
                getCarbonEmissionResponseTanZhongHe.consume_carbon_electricity_reduces_zero.Add(energy_Carbon_Emission.consume_carbon_electricity_reduces_zero);
                getCarbonEmissionResponseTanZhongHe.carbon_emission_conversion.Add(energy_Carbon_Emission.carbon_emissions_energy_total);

                
            }
            return getCarbonEmissionResponseTanZhongHe;
        }

        public List<energy_consumption_statistics> getStatistics(DateTime date, bool pieChart )
        {
            List<energy_consumption_statistics> energy_Consumptions_statistics = _energyConsumptionRepository.getStatistics(date,pieChart);
            return energy_Consumptions_statistics;
        }

        public List<CompareData> getCompareData(DateTime date, string category)
        {
            List<CompareData> compareDatas = _energyConsumptionRepository.getCompareData(date,category);
            return compareDatas;
        }
        public ReturnData getConsumptionStatistics(DateTime startTime, DateTime endTime, string category)
        {
            ReturnData compareDatas = _energyConsumptionRepository.getConsumptionStatistics( startTime,  endTime,  category);
            return compareDatas;
        }

        public ReturnData getExpensstatistics(DateTime startTime, DateTime endTime, string category)
        {
            ReturnData compareDatas  = _energyConsumptionRepository.getConsumptionStatistics(startTime, endTime, category);
            return compareDatas;
        }

        public List<pieData> getPieData(DateTime date)
        {
            List<pieData> pieData = _energyConsumptionRepository.getPieData(date);
            return pieData;
        }

        public List<GramData> getExpenseData(DateTime date)
        {
            List<GramData> gramData = _energyConsumptionRepository.getExpenseData(date);
            return gramData;
        }

        public List<GramData> getConsumptionData(DateTime date)
        {
            List<GramData> gramData = _energyConsumptionRepository.getConsumptionData(date);
            return gramData;
        }
    }
}
